package com.heima.wemedia.fegin;

import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.dtos.NewsAuthDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmSensitiveService;
import com.heima.wemedia.service.WmUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
@RestController
public class WemediaClient implements IWemediaClient {

    @Autowired
    private WmSensitiveService wmSensitiveService;

    //查看文章列表
    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    @RequestMapping( "/api/v1/sensitive/del/{id}")
    public ResponseResult delSensitive(Integer id) {
        return ResponseResult.okResult(wmSensitiveService.delSensitive(id));
    }

    /**
     * 查询敏感词
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/sensitive/list")
    public ResponseResult findList(SensitiveDto dto) {
        return ResponseResult.okResult(wmSensitiveService.findList(dto));
    }

    /**
     * 保存敏感词
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/sensitive/save")
    public ResponseResult save(@RequestBody WmSensitive dto) {
        return ResponseResult.okResult(wmSensitiveService.saveWmSensitive(dto));
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/sensitive/update")
    public ResponseResult update(WmSensitive dto) {
        return ResponseResult.okResult(wmSensitiveService.updateWmSensitive(dto));
    }

    /**
     * 查询文章
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/news/findList")
    public ResponseResult wmNewsList(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findList(dto);
    }

    /**
     * 查看详细信息
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one_vo/{id}")
    public ResponseResult detailsQuery(@PathVariable("id")Integer id) {
        return wmNewsService.detailsQuery(id);
    }

    /**
     * 审核是否成功
     * @return
     */
    @Override
    @PostMapping("/yesOrNo")
    public ResponseResult yesOrNo(@RequestBody NewsAuthDto dto) {

        return wmNewsService.yesOrNo(dto.getId(),dto.getStatus(),dto.getMsg());
    }

    @Autowired
    private WmChannelService wmChannelService;


    /**
     * 删除频道
     * @param id
     * @return
     */
    @GetMapping("/api/v1/channel/del/{id}")
    @Override
    public ResponseResult delChannel(@PathVariable Integer id){
        return wmChannelService.delChannel(id);
    }

    /**
     * wm端查询频道远程调用
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/channel/list")
    public ResponseResult listChannel(ChannelDto dto) {
        return wmChannelService.listChannel(dto);
    }

    /**
     * 保存频道信息
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/channel/save")
    public ResponseResult saveChannel(WmChannel dto) {
        return wmChannelService.saveChannel(dto);
    }

    /**
     * 更新频道信息
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/api/v1/channel/update")
    public ResponseResult updateChannel(WmChannel dto) {
        return wmChannelService.updateChannel(dto);
    }

    @Autowired
    private WmUserService wmUserService;

    @Override
    @PostMapping("/api/v1/wmUser/add")
    public ResponseResult addWmUser(ApUser apUser) {
        if(null==apUser){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setCreatedTime(new Date());
        return wmUserService.save(wmUser) ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
